<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button class="edit-btn" round size="mini" plain type="danger" @click="isEdit = !isEdit">{{isEdit?'完成':'编辑'}}</van-button>
    </van-cell>

    <van-grid class="my-grid" :gutter="10">
      <van-grid-item class="grid-item" v-for="(item, index) in myChannel" :key="item.id" @click="clickChannel(item, index)">
        <van-icon v-show="isEdit && !flexChannel.includes(item.id)" class="closeIcon" slot="icon" @click="removeClose(item, index)" name="close"></van-icon>
        <span class="text" :class="index === cur? 'curColor':null" slot="text" >{{item.name}}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>

    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item class="grid-item" icon="plus" v-for="item in recommendList" :key="item.id" :text="item.name" @click="removeItem(item)" />
    </van-grid>
  </div>
</template>

<script>
import { allChannelList, addUserChannel, delUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  props: {
    myChannel: {
      type: Array,
      require: true
    },
    cur: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      allChannel: [],
      isEdit: false,
      flexChannel: [0]
    }
  },
  created () {
    this.getAllChannel()
  },
  methods: {
    // 获取所有频道列表
    async getAllChannel () {
      try {
        const { data: res } = await allChannelList()
        // console.log(res, 888)
        this.allChannel = res.data.channels
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    // 移除推荐频道项
    async removeItem (item) {
      this.myChannel.push(item)
      if (this.user) {
        // 接口 后台处理
        try {
          const { data: res } = await addUserChannel({
            id: item.id,
            seq: this.myChannel.length
          })
          console.log(res)
          this.$toast('成功添加频道')
        } catch (err) {
          console.log(err)
          this.$toast('频道添加失败，请重新添加')
        }
      } else {
        // 本地存储
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    },
    // 移除我的频道中的某个频道
    async removeClose (item, index) {
      if (index <= this.cur) {
        // 删除选中项前面的项，选中项会因为 index 改变而改变，从而发生错误，所以之前的需要处理 cur 是父组件传过来的值，需要通过 this.$emit() 修改
        this.$emit('changeCur', this.cur - 1, true)
      }
      this.myChannel.splice(index, 1)
      if (this.user) {
        // 用户登录 数据更新到后端
        try {
          const { data: res } = await delUserChannel(item.id)
          console.log(res)
        } catch (err) {
          console.log(err)
        }
      } else {
        // 用户未登录 存储本地
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    },
    // 点击我的频道 跳转到首页 关闭按钮；将值传递给 cur
    clickChannel (item, index) {
      if (this.isEdit) return
      this.$emit('changeCur', index, false)
    }
  },
  computed: {
    ...mapState(['user']),
    recommendList () {
      return this.allChannel.filter(item => {
        return !this.myChannel.find(it => {
          return item.id === it.id
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #cccccc;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text, .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-grid-item__content {
        .curColor {
          color: #e36868;
        }
        .closeIcon {
          position: absolute;
          top: -40px;
          right: -90px;
          z-index: 2;
          font-size: 30px;
          color: #222;
        }
      }
    }
  }
  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
